package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WrapperUtil;
import com.ruoyi.common.vo.Result;
import com.ruoyi.system.domain.ProductKey;
import com.ruoyi.web.controller.vo.AddProductKeyVo;
import com.ruoyi.web.controller.vo.UpdateKeyVo;
import com.ruoyi.web.service.ISysProductKeySevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/productKey")
@Api(tags="商品卡密")
public class SysProductKeyController {

    @Autowired
    private ISysProductKeySevice productKeySevice;

    @GetMapping("/list")
    @ApiOperation(value="商品卡密-列表查询", notes="商品卡密-列表查询")
    @Log(title = "商品卡密-列表查询", businessType = BusinessType.OTHER)
    public Result<?> list(ProductKey productKey,
                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        LambdaQueryWrapper<ProductKey> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ProductKey::getCreateTime);
        Page<ProductKey> page = new Page<>(pageNo, pageSize);
        queryWrapper.eq(ProductKey::getProductId, productKey.getProductId());
        queryWrapper.like(StringUtils.isNoneBlank(productKey.getCardKey()), ProductKey::getCardKey, productKey.getCardKey());
        return Result.OK(productKeySevice.page(page, queryWrapper));
    }

    @PostMapping("/add")
    @ApiOperation(value="商品卡密-批量添加", notes="商品卡密-批量添加")
    @Log(title = "商品卡密-批量添加", businessType = BusinessType.INSERT)
    public Result<?> addProductKey(@RequestBody List<AddProductKeyVo> addProductKeyVos) {
        Long count = productKeySevice.addProductKey(addProductKeyVos);
        return Result.OK("添加成功数：" + count);
    }

    @PostMapping("/delete")
    @ApiOperation(value="商品卡密-删除", notes="商品卡密-删除")
    @Log(title = "商品卡密-删除", businessType = BusinessType.DELETE)
    public Result<?> delete(@RequestBody List<Long> ids) {
        productKeySevice.delete(ids);
        return Result.OK("删除成功");
    }

    @PostMapping("/updateKey")
    @ApiOperation(value="商品卡密-修改卡密Key", notes="商品卡密-修改卡密Key")
    @Log(title = "商品卡密-修改卡密Key", businessType = BusinessType.UPDATE)
    public Result<?> updateKey(@RequestBody UpdateKeyVo updateKeyVo) {
        productKeySevice.updateKey(updateKeyVo);
        return Result.OK("卡密修改成功");
    }

    @PostMapping("/updateSellStatus")
    @ApiOperation(value="商品卡密-修改卡密状态为已卖出", notes="商品卡密-修改卡密状态为已卖出")
    @Log(title = "商品卡密-修改卡密状态为已卖出", businessType = BusinessType.UPDATE)
    public Result<?> updateSellStatus(@RequestBody List<Long> ids) {
        productKeySevice.updateSellStatus(ids);
        return Result.OK("卡密状态修改成功");
    }

}
